var check1 = '0'
var verifyCode = ''; //服务器返回的验证码
var serviceUrl = $api.getStorage('serviceUrl');



apiready = function() {


}

function doLogin() {
	var account = $api.val($api.byId("account")).trim();
	var password = $api.val($api.byId("password")).trim();
	if (!account || !password) {
		api.toast({
			msg: '用户名或密码不能为空',
			duration: 2000,
			location: 'bottom'
		});
		return;
	}
	do_ajax('/api/commuser/login', 'post', JSON.stringify({
		account: account,
		password: password
	}), function(result, err) {
		api.hideProgress();
		if (result.code === 0) {
			if (result.entity) {
				$api.setStorage('account', account);
				$api.setStorage('password', password);
				$api.setStorage('member', result.entity);
				$api.setUserInf(result.entity);
				api.closeWin();
			} else {
				console.error("登陆的Entity 为空");
			}
		} else {
			api.toast({
				msg: result.msg,
				duration: 2000,
				location: 'bottom'
			});
		}
	});
}

//登陆
function doLogins() {
	account = $api.val($api.byId("account")).trim();
	password = $api.val($api.byId("password")).trim();
	if (account == '' || password == '') {
		toast('登陆信息为空!');
		return;
	}

	do_ajax('/app/member/login', 'post', {
		account: account,
		password: password
	}, function(result, err) {
		if (result.resultCode == '0') {

			//发送用户登陆退出监听事件
			api.sendEvent({
				name: 'refreshuser',
			});

			api.sendEvent({
				name: 'refreshUserinfo',
			});

			api.sendEvent({
				name: 'refreshProperty',
			});

			//    alert(JSON.stringify(result));

			//		getMemberInfo(account);

			$api.setStorage('member', result.member); //用户基本信息
			$api.setStorage('member2', result.member2); //小区用户信息


			$api.setStorage('account', account);
			$api.setStorage('password', password);
			setTimeout(function() {
				api.sendEvent({
					name: 'loginSuccess',
					extra: {
						account: account,
						password: password
					}
				});
				//	api.execScript({name: 'index',script: 'randomSwitchBtn(this,"index",0);getCartNum();'});
				api.execScript({
					name: 'index',
					script: 'randomSwitchBtn(this,"index",0);'
				});
				api.closeWin();
			}, 300);

			toast('登陆成功');

		} else {
			toast(result.datas);
		}
		api.hideProgress();

	});

};



//获取验证码
function getCode() {

	var mobile = $api.val($api.byId('mobile'));
	var mobilereg = /^0?(13[0-9]|15[012356789]|18[02356789]|14[57])[0-9]{8}$/;
	if (!mobilereg.test(mobile)) {
		toast('手机号格式错误!');
		check1 = "1";
		$api.attr($api.byId("mobile"), 'style', 'border:1px solid red');
		return;
	} else {
		GetValidateCodeClick();

	}


};


//计时器
var i = 60;
var timer;

function runTime() {
	i--;
	$api.text($api.byId('sec_reload'), "" + i);
	if (i <= 0) {
		verifyCode = '';
		$api.addEvt($api.byId('btn_getValiData'), 'click', function() {
			GetValidateCodeClick()
		});
		$api.removeAttr($api.byId('btn_getValiData'), 'disabled')


		$api.css($api.dom(".tip"), 'display:none');
		i = 60;
		clearTimeout(timer);
	}
	timer = setTimeout("runTime()", 1000);
}


function GetValidateCodeClick() {

	var mobile = $api.val($api.byId('mobile'));

	$api.rmEvt($api.byId('btn_getValiData'), 'click', function() {});
	$api.attr($api.byId('btn_getValiData'), 'disabled', true);

	if (timer) {
		clearTimeout(timer);
	}
	runTime();
	i = 60;
	$api.css($api.dom(".tip"), 'color:red;display:block;');

	do_ajax('/app/sendcode', 'post', {
		'mobile': mobile
	}, function(resData, err) {
		api.hideProgress();
		if (resData.resultCode == '0') {
			verifyCode = resData.code;
			toast(resData.data);
		} else {
			toast(resData.data);

			$api.addEvt($api.byId('btn_getValiData'), 'click', function() {
				GetValidateCodeClick()
			});
			$api.attr($api.byId('btn_getValiData'), 'disabled', 'disabled');
			$api.removeCls($api.byId('btn_getValiData'), 'ggl_ny_btn_disable');

			$api.css($api.dom(".tip"), 'color:red;display:none;');
			clearTimeout(timer);
		}
	});

};


//提交手机号

function dosubmitPhone() {
	var mobile = $api.val($api.byId('mobile'));
	var verify = $api.val($api.byId('verify'));
	var source = api.deviceModel;

	if (mobile == '') {
		toast('请填写手机号码！');
		return;
	}

	if (verify == '') {
		toast('请填写验证码!');
		return;
	}


	if (verify != verifyCode) {
		toast('验证码错误!');
		return;
	}


	do_ajax('/app/member/reg', 'post', {
		'account': mobile,
		'mobile': mobile,
		'source': source
	}, function(result, err) {
		api.hideProgress();
		if (result.resultCode == '0') {
			$api.attr($api.byId("reg1"), 'class', 'am-hide');
			$api.attr($api.byId("reg2"), 'class', 'am-show');
			$api.val($api.byId('memberID'), result.memberID);
			alert('注册成功');
		} else {
			toast(result.datas);
		}

	});

};


//提交修改信息
function doReg() {

	var password = $api.val($api.byId('password'));
	var password2 = $api.val($api.byId('password2'));
	//	  var  nickname=$api.val($api.byId('nickname'));

	var memberID = $api.val($api.byId('memberID'));

	if (password == "" || password2 == "") {
		toast("密码不能为空！");
		return;
	}

	if (password != password2) {
		toast("密码不一致！");
		return;
	}


	do_ajax('/app/member/update', 'post', {
		'password': password,
		'memberID': memberID
	}, function(result, err) {
		api.hideProgress();
		if (result.resultCode == '0') {
			toast("注册成功！");
			api.closeWin();
		} else {
			toast(result.datas);
			return;
		}
	});


};